package controller;

import hibernate.Driverstab;
import hibernate.DriverstabDAO;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.websocket.MessageHandler;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.RemoteEndpoint.Basic;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.json.JSONObject;




@ServerEndpoint("/websocket")
public class WebServerEndpoint {
	private String parkID;
	private static List<Session>sessions = new ArrayList<Session>();
	private static List<String>parks = new ArrayList<String>();
	private  static HashMap<String, Session> parkMap=new HashMap<String, Session>();
	
	@OnOpen
	public void open(Session session){
		String queryString = session.getQueryString();
		parkID = queryString.split("=")[1];
		
		this.sessions.add(session);
		this.parks.add(parkID);	
		
		
		this.parkMap.put(parkID, session);
	}
	
	
	
	@OnClose
	public void close(Session session){
		this.sessions.remove(session);
		this.parks.remove(this.parkID);
		this.parkMap.remove(this.parkID);
		//System.out.println(parks);
		
	}
	
	public static  void orderRequest (int userid,int parkid,int orderid)
	{
		try
		{
//			System.out.println("ordre request"+parkMap.size());
//			System.out.println("order request "+parkid);
			Session session = parkMap.get(Integer.toString(parkid));
//			System.out.println(session.getId());
			session.getBasicRemote().sendText("您有新的订单！");
			/*RemoteEndpoint.Basic remote=userMap.get(userid);
			JSONObject resultJson=new JSONObject();
			resultJson.put("op","orderAccept");
			remote.sendText(resultJson.toString());*/
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}


	
}
